//
//  FenRunXiangQingViewController.m
//  EJLX
//
//  Created by adc on 15/11/17.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "FenRunXiangQingViewController.h"
#import "FenRunTableViewCell.h"
#import "SecondFenRunLeiJiShouRu.h"
#import "WoDeFenDianViewController.h"
@interface FenRunXiangQingViewController ()

@end

@implementation FenRunXiangQingViewController
-(void)viewWillAppear:(BOOL)animated
{
    //[GMDCircleLoader setOnView:self.view withTitle:@"..." animated:YES];
    [HttpsManager requestFenRunLeiJiShouRu:@"1" comoletion:^(FebRunLeiJiShouRu *fenRun) {
        if (fenRun!=nil)
        {
            _fenRun=fenRun;
            _dataAry=_fenRun.listAry;
            NSLog(@"zzz%lu",(unsigned long)_dataAry.count);
            __weak FenRunXiangQingViewController *weakSelf = self;
            [tabelView addPullToRefreshWithActionHandler:^{
                NSLog(@"当进行下拉刷新执行该方法");
                [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
            }];
            
            [tabelView addInfiniteScrollingWithActionHandler:^{
                [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
            }];

            [tabelView reloadData];
            [self bigView];
        }
        else
        {
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }];

    
}
- (void)requestEnd{
    [tabelView.pullToRefreshView stopAnimating];
    
    [tabelView.infiniteScrollingView stopAnimating];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    
    [self.view addSubview:view];
    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30,30)];
    [backBut setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];
//    UISearchBar*searchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(50, 5, WIDE-100, 34)];
//    searchBar.placeholder=@"用户姓名／电话";
//    searchBar.delegate=self;
//    [view addSubview:searchBar];
    UIButton*fenDianBut=[[UIButton alloc]initWithFrame:CGRectMake(WIDE-50, 10,40, 24)];
    [fenDianBut setTitle:@"分店" forState:UIControlStateNormal];
    fenDianBut.font=[UIFont systemFontOfSize:12];
    [fenDianBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [fenDianBut addTarget:self action:@selector(fenDianBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:fenDianBut];
    for (int i=0;i<4;i++)
    {
        UIButton*but=[[UIButton alloc]initWithFrame:CGRectMake(0+WIDE/4*i, 64,WIDE/4, 40)];
        //NSArray*ary=[NSArray arrayWithObjects:@"全部", @"商户",@"升级",@"代理",nil];
        NSArray*ary=[[NSArray alloc]initWithObjects:@"全部", @"商户",@"升级",@"代理",nil];
        but.font=[UIFont systemFontOfSize:14];
        [but setTitle:ary[i] forState:UIControlStateNormal];
        [but setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        but.tag=i+1;
        [but addTarget:self action:@selector(butClcik:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:but];
        
    }
    label=[[UILabel alloc]initWithFrame:CGRectMake(0, 64+40,WIDE/4,2)];
    label.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:label];
    tabelView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64+40+5,WIDE ,HEIGHT-64-40-60) style:UITableViewStyleGrouped];
    tabelView.delegate=self;
    tabelView.dataSource=self;
    tabelView.rowHeight=80;
    tabelView.tableFooterView=[[UIView alloc]init];
    [self.view addSubview:tabelView];
    [tabelView registerNib:[UINib nibWithNibName:@"FenRunTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
//    UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(50, HEIGHT-80, 100, 20)];
//    label1.textColor=[UIColor redColor];
//    [self.view addSubview:label1];
//    UILabel*label2=[[UILabel alloc]initWithFrame:CGRectMake(50,HEIGHT-40, 100, 20)];
//    label2.text=@"总营业额";
//    label2.textColor=[UIColor grayColor];
//    [self.view addSubview:label2];
//    
//    UILabel*label3=[[UILabel alloc]initWithFrame:CGRectMake(WIDE-150,HEIGHT-80, 100, 20)];
//    label3.textColor=[UIColor redColor];
//    [self.view addSubview:label3];
//    UILabel*label4=[[UILabel alloc]initWithFrame:CGRectMake(WIDE-150,HEIGHT-40, 100, 20)];
//    label4.text=@"总贡献分润";
//    [self.view addSubview:label4];
   // [self bigView];
    
  //  [GMDCircleLoader hideFromView:self.view animated:YES];
    
    // Do any additional setup after loading the view, typically from a nib.
    
}
-(void)bigView
{
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0,HEIGHT-60, WIDE,60)];
   // view.backgroundColor=[UIColor redColor];
    [self.view addSubview:view];
        UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(40,10, 100, 20)];
        label1.textColor=[UIColor redColor];
    label1.text=[NSString stringWithFormat:@"%.2f",_fenRun.totalTurnover*0.01];
        [view addSubview:label1];
    label1.textAlignment=NSTextAlignmentCenter;
        UILabel*label2=[[UILabel alloc]initWithFrame:CGRectMake(50,30, 100, 20)];
        label2.text=@"总营业额";
        //label2.textColor=[UIColor grayColor];
        [view addSubview:label2];
    
        UILabel*label3=[[UILabel alloc]initWithFrame:CGRectMake(WIDE-150,10, 100, 20)];
        label3.textColor=[UIColor redColor];
    label3.text=[NSString stringWithFormat:@"%.2f",_fenRun.totalContribution*0.01];
    label3.textAlignment=NSTextAlignmentCenter;
        [view addSubview:label3];
        UILabel*label4=[[UILabel alloc]initWithFrame:CGRectMake(WIDE-150+5,30, 100, 20)];
        label4.text=@"总贡献分润";
        [view addSubview:label4];

    
}
-(void)butClcik:(UIButton*)sender

{
    [HttpsManager requestFenRunLeiJiShouRu:[NSString stringWithFormat:@"%ld",(long)sender.tag] comoletion:^(FebRunLeiJiShouRu *fenRun) {
        
        if (fenRun!=nil)
        {
            _fenRun=fenRun;
            _dataAry=_fenRun.listAry;
            [tabelView reloadData];
            [self bigView];
        }
        else
        {
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }

        
        
    }];
    
    [UIView animateWithDuration:1 animations:^{
        label.frame=CGRectMake(WIDE/4*(sender.tag-1), 64+40,WIDE/4,2);
    } completion:^(BOOL finished)
     {
         nil;
     }];
}
//-(CGFloat)getHeightByString:(NSString*)string   withFont:(UIFont*)font
//{
//    CGSize size = [string boundingRectWithSize:CGSizeMake(300, LONG_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
//    return size .width;
//}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
//返回按钮
-(void)backBut
{
    [self.navigationController popViewControllerAnimated:YES];
}
//分店
-(void)fenDianBut
{
    WoDeFenDianViewController*vc=[[WoDeFenDianViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondFenRunLeiJiShouRu*sec=_dataAry[indexPath.row];
    FenRunTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //if (indexPath.row==0)
    //{
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:sec.userAvatar] placeholderImage:[UIImage imageNamed:@"头像1"]];
    cell.label1.text=sec.role;
    cell.label2.text=sec.name;
    cell.numberLabel.text=sec.phone;
    cell.yingYeLabel.text=[NSString stringWithFormat:@"¥ %.2f",sec.sales*0.01];
    cell.fenRunLabel.text=[NSString stringWithFormat:@"¥ %.2f",sec.contribute*0.01];
    
       // cell.imageView.image=[UIImage imageNamed:@"头像.jpg"];
        return cell;
    //}
    //cell.imageView.image=[UIImage imageNamed:@"默认.jpg"];
   // return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
